package com.demo.apparel.mapper.uniapp;

import com.demo.apparel.entity.Match;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MatchMapper {

    /**
     * 根据用户ID和名称模糊查询总数
     * @param userId 用户ID
     * @param name 搭配名称，可为空
     * @return 总记录数
     */
    @Select({
            "<script>",
            "SELECT COUNT(*) FROM `match`",
            "WHERE user_id = #{userId}",
            "<if test='name != null and name != \"\"'>",
            "AND name LIKE CONCAT('%', #{name}, '%')",
            "</if>",
            "</script>"
    })
    long countByUserIdAndName(@Param("userId") String userId,
                              @Param("name") String name);

    /**
     * 根据用户ID和名称查询分页列表
     * @param userId 用户ID
     * @param name 搭配名称，可为空
     * @param offset 偏移量
     * @param limit 每页数量
     * @return 搭配列表
     */
    @Select({
            "<script>",
            "SELECT * FROM `match`",
            "WHERE user_id = #{userId}",
            "<if test='name != null and name != \"\"'>",
            "AND name LIKE CONCAT('%', #{name}, '%')",
            "</if>",
            "ORDER BY created_time DESC",
            "LIMIT #{offset}, #{limit}",
            "</script>"
    })
    List<Match> selectByUserIdAndName(@Param("userId") String userId,
                                      @Param("name") String name,
                                      @Param("offset") int offset,
                                      @Param("limit") int limit);

    /**
     * 根据ID查询搭配
     */
    @Select("SELECT * FROM `match` WHERE id = #{id}")
    Match selectById(@Param("id") String id);

    /**
     * 根据用户ID查询所有搭配
     */
    @Select("SELECT * FROM `match` WHERE user_id = #{userId} ORDER BY created_time DESC")
    List<Match> selectByUserId(@Param("userId") String userId);

    /**
     * 插入搭配记录
     */
    @Insert("INSERT INTO `match` (id, user_id, name, description, images, tags, created_time, updated_time) " +
            "VALUES (#{id}, #{userId}, #{name}, #{description}, #{images}, #{tags}, NOW(), NOW())")
    void insert(Match match);


    /** 查询所有用户的搭配总数，可按 name 搜索 */
    @Select({
            "<script>",
            "SELECT COUNT(*) FROM `match`",
            "WHERE 1=1",
            "<if test='name != null and name != \"\"'>",
            "AND name LIKE CONCAT('%', #{name}, '%')",
            "</if>",
            "</script>"
    })
    long countAllMatches(@Param("name") String name);

    /** 查询所有用户的搭配列表，可分页，可按 name 搜索 */
    @Select({
            "<script>",
            "SELECT * FROM `match`",
            "WHERE 1=1",
            "<if test='name != null and name != \"\"'>",
            "AND name LIKE CONCAT('%', #{name}, '%')",
            "</if>",
            "ORDER BY created_time DESC",
            "LIMIT #{offset}, #{limit}",
            "</script>"
    })
    List<Match> selectAllMatches(@Param("name") String name,
                                 @Param("offset") int offset,
                                 @Param("limit") int limit);
}

